package com.yun2.bms.controller.book;

import com.yun2.bms.model.Book;
import com.yun2.bms.model.Category;
import com.yun2.bms.service.IBookService;
import com.yun2.bms.service.ICategoryService;
import com.yun2.bms.service.impl.BookServiceImpl;
import com.yun2.bms.service.impl.CategoryServiceImpl;
import com.yun2.bms.utils.StringUtils;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * author: youli
 * qq: 27855162@qq.com
 * date: 2020/5/26
 */

public class AddBookController implements Initializable {

    private IBookService bookService = new BookServiceImpl();

    private ICategoryService categoryService = new CategoryServiceImpl();

    //图书模型数据集合，可以实时相应数据变化，无需刷新
    private ObservableList<Book> bookData = FXCollections.observableArrayList();

    public ObservableList<Book> getBookData() {
        return bookData;
    }

    public void setBookData(ObservableList<Book> bookData) {
        this.bookData = bookData;
    }

    @FXML
    private ComboBox<Category> cbCategoryId;

    @FXML
    private TextField tfBookNumber;

    @FXML
    private TextField tfPublisher;

    @FXML
    private Button btnCancel;

    @FXML
    private Button btnOK;

    @FXML
    private TextField tfAuthor;

    @FXML
    private TextField tfBookName;

    @FXML
    private TextField tfPrice;

    @FXML
    void add(ActionEvent event) {
        String bookName = this.tfBookName.getText();
        String bookNumber = this.tfBookNumber.getText();
        String author = this.tfAuthor.getText();
        String publisher = this.tfPublisher.getText();
        String price = this.tfPrice.getText();
        String info = "";
        boolean flag = true;
        if (StringUtils.isBlank(bookName)) {
            info = "书名不能为空！";
            flag = false;
        } else if (StringUtils.isBlank(bookNumber) || !StringUtils.isNumber(bookNumber)) {
            info = "图书数量必须是数字，且不能为空！";
            flag = false;
        } else if (StringUtils.isBlank(price) || !StringUtils.isNumber(price)) {
            info = "图书价格必须是数字，且不能为空！";
            flag = false;
        } else if (StringUtils.isBlank(author)) {
            info = "作者信息不能为空！";
            flag = false;
        } else if (StringUtils.isBlank(publisher)) {
            info = "出版社信息不能为空！";
            flag = false;
        } else if (cbCategoryId.getValue() == null) {
            info = "请选择图书分类！";
            flag = false;
        }
        Alert alert = null;
        if(!flag){
            //显示提示信息
            alert = new Alert(Alert.AlertType.ERROR);
        }else{
            Book book = new Book(cbCategoryId.getValue().getId(),bookName,Integer.valueOf(bookNumber),author,publisher,Double.valueOf(price));
            int id = this.bookService.add(book);
            System.out.println("id=============="+id);
            if(id > 0){
                book.setId(id);
                book.setCategoryName(cbCategoryId.getValue().getCategoryName());
                this.bookData.add(book);
                info = "添加图书成功！";
            }else{
                info = "添加图书失败！";
            }
            //关闭当前窗口
            closeWindow();
            //显示提示信息
            alert = new Alert(Alert.AlertType.INFORMATION);
        }
        //显示提示信息
        alert.setTitle("提示信息");
        alert.setHeaderText(info);
        alert.showAndWait();
    }

    @FXML
    void cancel(ActionEvent event) {
        this.closeWindow();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //加载所有图书分类数据,并设置选择监听
        loadCategories();

    }

    private void closeWindow() {
        Stage stage = (Stage) this.btnOK.getScene().getWindow();
        stage.close();
    }

    /**
     * 加载所有图书分类数据,并设置选择监听
     */
    private void loadCategories() {
        ObservableList<Category> categoryData = FXCollections.observableArrayList();
        List<Category> list = this.categoryService.getAll();
        categoryData.addAll(list);
        cbCategoryId.setItems(categoryData);
        cbCategoryId.setConverter(new StringConverter<Category>() {
            @Override
            public String toString(Category obj) {
                return String.format("%s", obj.getCategoryName());
            }

            @Override
            public Category fromString(String string) {
                System.out.println(string);
                return null;
            }
        });
        cbCategoryId.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                Category category = cbCategoryId.getValue();
                System.out.println(category.getId() + "--" + category.getCategoryName());
            }
        });
    }
}
